package com.example.smart_city_0829.activity.government;

import androidx.annotation.NonNull;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.gov.GovAdvList;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;

import java.util.List;
import java.util.Objects;

import retrofit2.Call;
import retrofit2.Response;

public class GovernmentTypeActivity extends BaseActivity {

    private int appealCategoryId;
    private String appealCategoryName;

    private Button pubAdv;
    private ListView lv;

    private int pageNum = 1;

    private ListAdapter listAdapter;
    private List<GovAdvList.RowsBean> data;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_government_type;
    }

    @Override
    protected void initView() {
        pubAdv = (Button) findViewById(R.id.pub_adv);
        lv = (ListView) findViewById(R.id.lv);
    }

    @Override
    protected void initData() {
        appealCategoryId = getIntent().getIntExtra("appealCategoryId",-1);
        appealCategoryName = getIntent().getStringExtra("appealCategoryName");
        Objects.requireNonNull(getSupportActionBar()).setTitle(appealCategoryName);

        api.getGovAdvListOfType(appealCategoryId, Constant.pageSize,pageNum).enqueue(new NetCallBack<GovAdvList>() {
            @Override
            public void onResponse(Call<GovAdvList> call,@NonNull Response<GovAdvList> response) {
                if (response.body() == null) return;
                data = response.body().getRows();
                if (listAdapter == null){
                    listAdapter = new ListAdapter(data,GovernmentTypeActivity.this);
                    lv.setAdapter(listAdapter);
                }else {
                    listAdapter.resetData(data);
                    listAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        pubAdv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 跳转至创建诉求页面
                final Intent intent = new Intent(GovernmentTypeActivity.this,GovernmentPubActivity.class);
                intent.putExtra("appealCategoryId",appealCategoryId);
                intent.putExtra("appealCategoryName",appealCategoryName);
                startActivity(intent);
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final Intent intent = new Intent(GovernmentTypeActivity.this, GovAdvDetailActivity.class);
                intent.putExtra("id",data.get(position).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "诉求分类";
    }

    public class ListAdapter extends CommonAdapter<GovAdvList.RowsBean,ListViewHolder>{

        public ListAdapter(List<GovAdvList.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(GovAdvList.RowsBean data, int position, ListViewHolder viewHolder, View convertView, Context context) {
            viewHolder.title.setText(String.valueOf(data.getTitle()));
            viewHolder.content.setText(data.getContent());
            viewHolder.tag1.setText("承办单位："+data.getUndertaker());
            viewHolder.tag2.setText("提交时间："+data.getUpdateTime());
            viewHolder.tag3.setText("处理状态:"+data.getState());

            Glide.with(GovernmentTypeActivity.this)
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .into(viewHolder.im);
        }

        @Override
        protected ListViewHolder getViewHolder() {
            return new ListViewHolder();
        }

        @Override
        protected void initView(ListViewHolder viewHolder, View convertView) {
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
            viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
            viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
        }
    }

    static class ListViewHolder{
        TextView title;
        ImageView im;
        TextView content;
        TextView tag1;
        TextView tag2;
        TextView tag3;
    }

}